package com.easygem.controller.front;

import io.swagger.annotations.Api;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;

/**
 * @author luoxi
 * @version 1.0
 * @className TemplateController
 * @Description TODO
 * @date 2025/1/3 16:59
 */
@Api(tags = "模版下载")
@RestController
@RequestMapping("/template")
public class TemplateController {

    @PostMapping("/download")
    public ResponseEntity<byte[]> downloadTemplate(@RequestBody Map<String, Object> requestBody) throws IOException {
        // 从请求体中获取传递过来的templateId值
        Object templateId = requestBody.get("templateId");
        ClassPathResource resource = new ClassPathResource(String.valueOf(templateId));
        byte[] bytes = Files.readAllBytes(resource.getFile().toPath());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return ResponseEntity.ok().headers(headers).body(bytes);
    }
}
